import { defineStore } from 'pinia'
import request from '@/utils/request'
import {CategoryItem, IApiRes, TopCategory} from '@/types/data'
import {topCategory} from '../constants'
const topCategoryList = topCategory.map(item=>({id:item,name:item}))
export default defineStore('category',{
   state:()=>{
    return{
      list:topCategoryList as CategoryItem[],
      topCategory:{} as TopCategory
    }
   },
   actions:{
    async getCategoryList(){
      // const res = await request.get<{msg:string,result:CategoryItem[]}>('/home/category/head')
      const res = await request.get<IApiRes<CategoryItem[]>>('/home/category/head')
      console.log(res.data.result);
      this.list=res.data.result
      
    },
    async getTopCategory(id:string){
      // const res = await request.get<{msg:string,result:CategoryItem[]}>('/home/category/head')
      const res = await request.get<IApiRes<TopCategory>>('/category',{params:{id:id}})
      // console.log(res);
      this.topCategory = res.data.result
    }
   },
   persist:{
    enabled:true
   }
})